$(function(){
	$("#addBtnTest").click(function(){
		var subId = $("#subId").val();
		var testType = $("#testType").val();
		var content = $("#content").val();
		var chooseA = $("#chooseA").val();
		var chooseB = $("#chooseB").val();
		var chooseC = $("#chooseC").val();
		var chooseD = $("#chooseD").val();
		var anwer;
		if(testType == 1){
			anwer = $("input[name='anwer']:checked").val()
		}else if(testType == 2){
			var obj = $("input[type='checkbox']:checked");
			var s="";
			$(obj).each(function(){
				s+=this.value;
			    }); 
			anwer = s;
		}
		
		if(content!=""){
			if(chooseA != ""&chooseB != ""&chooseC != ""&chooseD != ""){
				if(anwer != ""){
					$.ajax({
						type: 'POST',
						url: 'saveExamTest',
						data:{
							"subId":subId,
							"testType":testType,
							"content":content,
							"chooseA":chooseA,
							"chooseB":chooseB,
							"chooseC":chooseC,
							"chooseD":chooseD,
							"anwer":anwer
							},
						dataType: "json",
						success:function(date){
							 alert(date.massage);
							 location.reload();
						}
					});
				}else{
					alert("请至少选择一个选项");
				}
			}else{
				alert("请输入题目的答案");
			}
		}else{
			alert("提干不能为空！");
		}
		
		
	});
	
	$("#add").click(function(){
		var testType = $("#testType").val();
		if(testType == 1){
			$(".testTypeScore").text("(每题"+3+"分)")
			$('#addFormTest .anwer input:radio').show();
			$('#addFormTest .anwer input:checkbox').hide();
		}else if(testType == 2){
			$(".testTypeScore").text("(每题"+5+"分)")
			$('#addFormTest .anwer input:radio').hide();
			$('#addFormTest .anwer input:checkbox').show();
		}else{
			alert("无此试题类型！");
		}
	});
	
	//添加模态框
	
	$('#addFormTest select[id="testType"]').change(function(){
		var testType = $(this).val();
		if(testType == 1){
			$(".testTypeScore").text("(每题"+3+"分)")
			$('#addFormTest .anwer input:radio').show();
			$('#addFormTest .anwer input:checkbox').hide();
		}else if(testType == 2){
			$(".testTypeScore").text("(每题"+5+"分)")
			$('#addFormTest .anwer input:radio').hide();
			$('#addFormTest .anwer input:checkbox').show();
		}else{
			alert("无此试题类型！");
		}
	});
	
	//修改模态框
	$('#editFormTest select[class="testType"]').change(function(){
		var testType = $(this).val();
		if(testType == 1){
			$(".editTestTypeScore").text("(每题"+3+"分)");
			$('#editFormTest .anwer input:radio').show();
			$('#editFormTest .anwer input:checkbox').hide();
		}else if(testType == 2){
			$(".editTestTypeScore").text("(每题"+5+"分)");
			$('#editFormTest .anwer input:radio').hide();
			$('#editFormTest .anwer input:checkbox').show();
		}else{
			alert("无此试题类型！");
		}
	});
	
	
	$(".show-test").click(function(){
		var testId = $(this).parent().siblings().eq(0).text();
		$.ajax({
			type: 'POST',
			url: 'getTestById',
			data:"testId="+testId,
			dataType: "json",
			success:function(date){
				$("#showTestModal .sub-name").text(date.subName);
				$("#showTestModal .content").text(date.content);
				$("#showTestModal .test-type").text(date.examTestType.testType);
				$("#showTestModal .test-type-score").text(date.examTestType.testTypeScore);
				$("#showTestModal .choose-a").text(date.chooseA);
				$("#showTestModal .choose-b").text(date.chooseB);
				$("#showTestModal .choose-c").text(date.chooseC);
				$("#showTestModal .choose-d").text(date.chooseD);
				$("#showTestModal .answer").text(date.answer);
			}
		});
		$("#showTestModal").modal("show");
	});
	
	var id;
	$(".edit-test").click(function(){
		var testId = $(this).parent().siblings().eq(0).text();
		id=testId;
		$.ajax({
			type: 'POST',
			url: 'getTestById',
			data:"testId="+testId,
			dataType: "json",
			success:function(date){
				var testType = date.examTestType.id;
				if(testType == 1){
					$(".editTestTypeScore").text("(每题"+3+"分)");
					$('#editFormTest .anwer input:radio').show();
					$('#editFormTest .anwer input:checkbox').hide();
				}else if(testType == 2){
					$(".editTestTypeScore").text("(每题"+5+"分)");
					$('#editFormTest .anwer input:radio').hide();
					$('#editFormTest .anwer input:checkbox').show();
				}else{
					alert("无此试题类型！");
				}
				
				$("#editFormTest .subId").val(date.subId);
				$("#editFormTest .testType").val(date.examTestType.id);
				$("#editFormTest .content").val(date.content);
				$(".chooseA").siblings("input").val(date.chooseA);
				$(".chooseB").siblings("input").val(date.chooseB);
				$(".chooseC").siblings("input").val(date.chooseC);
				$(".chooseD").siblings("input").val(date.chooseD);
				if(date.examTestType.id ==1){
					if(date.answer == "A"){
						$("input[name='editRadio'][value=A]").prop("checked",true);
					}else if(date.answer == "B"){
						$("input[name='editRadio'][value=B]").prop("checked",true);
					}else if(date.answer == "C"){
						$("input[name='editRadio'][value=C]").prop("checked",true);
					}else if(date.answer == "D"){
						$("input[name='editRadio'][value=D]").prop("checked",true);
					}
				}else if(date.examTestType.id ==2){
					var a = /A/;
					var b = /B/;
					var c = /C/;
					var d = /D/;
					var anwer = date.answer;
					for(var i=-4;i<anwer.length;i++){
						if(a.test(anwer)){
							$("input[name='editCheckbox'][value=A]").prop("checked",true);
						}else if(b.test(anwer)){
							$("input[name='editCheckbox'][value=B]").prop("checked",true);
						}else if(c.test(anwer)){
							$("input[name='editCheckbox'][value=C]").prop("checked",true);
						}else if(d.test(anwer)){
							$("input[name='editCheckbox'][value=D]").prop("checked",true);
						}else{
							$("input[name='editCheckbox'][value=D]").prop("checked",false);
						}
						anwer = anwer.substring(1);
					}
				}
			}
		});
		$("#editTestModal").modal("show");
	});
	
	$(".editTestType").click(function(){
		
		var testId = $(".edit-test").parent().siblings().eq(0).text();
		var subId = $("#editFormTest .subId").val();
		var testType = $("#editFormTest .testType").val();
		var content =  $("#editFormTest .content").val();
		var chooseA = $(".chooseA").siblings("input").val();
		var chooseB = $(".chooseB").siblings("input").val();
		var chooseC = $(".chooseC").siblings("input").val();
		var chooseD = $(".chooseD").siblings("input").val();
		var anwer;
		if(testType == 1){
			anwer = $("input[name='editRadio']:checked").val()
		}else if(testType == 2){
			var obj = $("input[name='editCheckbox']:checked");
			var s="";
			$(obj).each(function(){
				s+=this.value;
			    }); 
			anwer = s;
		}
		
		if(content!=""){
			if(chooseA != ""&chooseB != ""&chooseC != ""&chooseD != ""){
				$.ajax({
					type: 'POST',
					url: 'editExamTest',
					data:{
						"id":id,
						"subId":subId,
						"testType":testType,
						"content":content,
						"chooseA":chooseA,
						"chooseB":chooseB,
						"chooseC":chooseC,
						"chooseD":chooseD,
						"anwer":anwer
						},
					dataType: "json",
					success:function(date){
						 alert(date.massage);
						 location.reload();
					}
				});
			}else{
				alert("请输入题目的答案");
			}
		}else{
			alert("提干不能为空！");
		}
	});
});